package com.webservice.service.impl;

import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.webservice.dao.Pub;
import com.webservice.dao.PubBaseDao;
import com.webservice.service.IBaseService;
import com.webservice.utils.BillTypeUtil;
import com.webservice.utils.DateTimeTool;
import com.webservice.utils.MyApplicationContextUtil;
import com.webservice.utils.UUIDUtil;
import com.webservice.utils.VOXMLMapping;
import com.webservice.utils.XmlCheck;
import com.webservice.vo.impl.ReturnMsgVo;

@Service("webServiceHandle")
public class WebServiceHandle {
	
	private static Logger logger = Logger.getLogger(WebServiceHandle.class);
	@Autowired
	@Qualifier("pubBaseDao")
	private PubBaseDao pubDaseDao;
	
	public PubBaseDao getPubDaseDao() {
		pubDaseDao=(PubBaseDao) MyApplicationContextUtil.getContext().getBean("pubBaseDao");
		return pubDaseDao;
	}

	/***
	 * 从上下文中得到实例
	 * @param pubDaseDao 
	 */
	public void setPubDaseDao(PubBaseDao pubDaseDao) {
		this.pubDaseDao = (PubBaseDao) MyApplicationContextUtil.getContext().getBean("pubBaseDao");
	}

	/***
	 * 
	 * <b> 功能 : 共用业务数据上传接口，提供外系统到b2b的数据上传功能 </b>
	 * @param xml 数据
	 * @param action_type 业务类型
	 * @throws IOException 
	 * @throws Exception 
	 * @作者  Andy
	 * @创建日期 2013-12-4
	 */
	public String upLoad(String xml, String action_type,String ip) throws IOException  {
		return dynamicCall(xml, action_type,ip);
	}

	/**
	 *  <b> 动态调用访问接口 </b>
	 * 处理过程：1.判断ip是否存在
	 *  	  2.校验billtype是否存在
	 *  	  3.校验xml是否合法
	 * @param xml
	 * @param action_type
	 * @return
	 * @throws IOException 
	 */
	private String dynamicCall(String xml, String action_type,String ip) throws IOException {
		try {
			logger.debug(action_type+" 开始处理______________"+ip);
			String intoUUID = UUIDUtil.getUUID();
			String outUUID = UUIDUtil.getUUID();
			// 成对标志
			String pairflag = DateTimeTool.getDateTimeString(new Date(),"yyyymmddHHmmss:sss").replace(":", "");
			String returnxml = "";
			if(null==action_type)  returnxml = Pub.returnInfo(null,-1,"失败：业务类型为空",null);
			if(null==BillTypeUtil.getMsg(action_type))returnxml = Pub.returnInfo(null,-1,"失败：业务类型不存在",null);
			String checkxml=XmlCheck.valid(action_type, xml);
			if(null!=checkxml)returnxml = Pub.returnInfo(null,-1,"失败："+checkxml,null);
			// 判断是否处理失败
			if(returnxml != null && !"".equals(returnxml)){
				Pub.insertLog(this.getPubDaseDao(), intoUUID, ip, judgeBusinessType(action_type), "", xml, "into",pairflag,"0");
				Pub.insertLog(this.getPubDaseDao(), outUUID, ip, judgeBusinessType(action_type), "", returnxml, "out",pairflag,"1");
				ReturnMsgVo rmv =  (ReturnMsgVo) VOXMLMapping.fromXML(returnxml, ReturnMsgVo.class, null);
				Pub.insertDealResultLog(this.getPubDaseDao(), outUUID, "WMS", "B2B", "处理异常", rmv.getMsg());
				
				return returnxml;
			}else{
				IBaseService baseService=(IBaseService) MyApplicationContextUtil.getContext().getBean(action_type);
				
				return baseService.returnData(xml, this.getPubDaseDao(), ip);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  Pub.returnInfo(null,-1,"失败："+e.getMessage(),null);
		}
	}
	
	/**
	 * 判断业务类型
	 * @param action_type
	 * @return
	 */
	private String judgeBusinessType(String action_type){
		switch (action_type) {
		case "B1001":
			return "09";
		case "B1002":
			return "13";
		case "B1003":
			return "14";
		case "B1004":
			return "10";
		case "F0001":
			return "11";
		case "F0002":
			return "12";
		case "B1005":
			return "20";
		default:
			return "";
		}
	}
	/***
	 * 
	 * <b> 功能 : 共用业务数据上传接口，提供外系统到b2b的数据下载功能 </b>
	 * @param xml 数据
	 * @param action_type 业务类型
	 * @throws IOException 
	 * @作者  Andy
	 * @创建日期 2013-12-4
	 */
	public String downLoad(String xml, String action_type,String ip) throws IOException {
		return dynamicCall(xml, action_type,ip);
	}
}
